﻿@inherits InputBase<int?>

<input type="text"
       class="input @(CurrentValue != null && (CurrentValue < Min || CurrentValue > Max) ? "border-accent2-900 bg-error" : "border-grey-700 bg-dark-550")"
       style="@Style"
       @bind-value="@CurrentValueAsString"
       @bind-value:event="onchange"/>

@code
{
    public enum Variant
    {
        Small,
        Normal,
        Stretch
    }

    [Parameter]
    public Variant Size { get; set; }

    [Parameter]
    public int Min { get; set; }

    [Parameter]
    public int Max { get; set; }

    private string Style => Size switch
    {
        Variant.Small => "width: 80px;",
        Variant.Normal => "",
        Variant.Stretch => "width: 100%;",
        _ => ""
        };

    protected override bool TryParseValueFromString(string value, out int? result, out string validationErrorMessage)
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            result = null;
            validationErrorMessage = null;
            return true;
        }

        if (int.TryParse(value, out var parsedInt))
        {
            validationErrorMessage = null;
            result = parsedInt;
            return true;
        }

        result = 0;
        validationErrorMessage = "This field must be a numeric value";
        return false;
    }
}